package joney.main.views.table;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.TableItem;

import joney.main.utils.JoneyUtil;
import joney.model.DataModel;
import joney.model.Expense;

public class ExpenseContentProvider implements IStructuredContentProvider, IExpenseListViewer {

	private TableViewer tableViewer;
	private ExpenseList elist;
	
	
	
	public ExpenseContentProvider(TableViewer tableViewer, ExpenseList elist)
	{
		this.tableViewer = tableViewer;
		this.elist = elist;
	}
	
	public void handleCustomCellRendering()
	{
		TableItem ti = tableViewer.getTable().getItem(tableViewer.getTable().getItemCount()-1);
		
		//draw stripes
		if(tableViewer.getTable().getItemCount() %2 ==0){
			ti.setBackground(new Color(JoneyUtil.getDisplay(),238,233,233));
			//ti.setForeground(JoneyUtil.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		}
		else{
			
			ti.setBackground(JoneyUtil.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		}
		
	}
	
	
	public void addExpense(Expense e) {
		tableViewer.add(e);
		handleCustomCellRendering();
	}

	public void removeExpense(Expense e) {
		tableViewer.remove(e);
		//fire listeners
		DataModel.fireExpenseRemoved(e);
		
	}

	public void updateExpense(Expense e) {
		tableViewer.update(e, null);
	}

	public Object[] getElements(Object inputElement) {
		return elist.getExpenses().toArray();
	}

	public void dispose() {
		elist.removeChangeListener(this);
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if (newInput != null)
			((ExpenseList) newInput).addChangeListener(this);
		if (oldInput != null)
			((ExpenseList) oldInput).removeChangeListener(this);
	}
}
